package com.ndp.fb.walle.business.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.rdb.api.EcTemplateElementRdbService;
import com.ndp.fb.rdb.api.EcTemplateElementStyleRdbService;
import com.ndp.fb.rdb.model.EcTemplate;
import com.ndp.fb.rdb.model.ectemplate.EcTemplateElementStyle;
import com.ndp.fb.walle.business.EcTemplateElementStyleService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by sisi.chen on 2016/7/12.
 */
@Component
public class EcTemplateElementStyleServiceImpl implements EcTemplateElementStyleService {
    @Reference
    private EcTemplateElementStyleRdbService ecTemplateElementStyleRdbService;

    @Reference
    EcTemplateElementRdbService ecTemplateElementRdbService;

    private final Logger logger = LoggerFactory.getLogger(EcTemplateMaterielServiceImpl.class);

    @Override
    public synchronized Set<Integer> bindNewTemplateElementStyleGroups(Long templateId, Set<Integer> newStyleGroups) {
        List<EcTemplateElementStyle> bindStyleGroups = ecTemplateElementStyleRdbService.findByTemplateId(templateId);

        Set<Integer> bindedGroups = new HashSet<>();
        for (EcTemplateElementStyle bind : bindStyleGroups){
            bindedGroups.add(bind.getElementStyleGroupId());
        }
        int bindesNum = bindedGroups.size();

        //获取未绑定过的新样式组
        List<EcTemplateElementStyle> needBindedGroups = new ArrayList<>();
        Set<Integer> canSaveGroups = new HashSet<>();
        for (Integer newGroup : newStyleGroups){
            if (bindedGroups.contains(newGroup) ){
                //如果当前样式组绑定过，则当前样式组下的元素可以保存
                canSaveGroups.add(newGroup);
            } else {
                if (bindesNum < 5){
                    //如果当前样式组未绑定过，且模板下的样式组个数还未满5个，则当前样式组下的元素可以保存且需要绑定新的样式组与模板之间的关系
                    EcTemplateElementStyle ecTemplateElementStyle = new EcTemplateElementStyle();
                    ecTemplateElementStyle.setTemplateId(templateId);
                    ecTemplateElementStyle.setElementStyleGroupId(newGroup);
                    needBindedGroups.add(ecTemplateElementStyle);

                    canSaveGroups.add(newGroup);
                    bindesNum++;
                }
            }
        }

        if (!CollectionUtils.isEmpty(needBindedGroups)) {
            ecTemplateElementStyleRdbService.batchSave(needBindedGroups);
        }
        return canSaveGroups;
    }

    @Override
    public List<EcTemplateElementStyle> findByTemplateId(Long templateId) {
        return ecTemplateElementStyleRdbService.findByTemplateId(templateId);
    }

    @Override
    public Integer bindTemplatesByStyleGroupId(Integer groupId, Set<String> templateIds, Long orgId) {
        List<EcTemplateElementStyle> needAddList = new ArrayList<>();
        List<EcTemplateElementStyle> needDeleteList = new ArrayList<>();
        List<Long> addIds = new ArrayList<>();
        if(CollectionUtils.isEmpty(templateIds)){
            return 0;
        }
        //库中有的  dataIds  [1,2,3]
        List<EcTemplate> dataIds = this.findTemplateByStyleGroupIdAndOrgId(groupId, orgId);
        // templateIds  [2,3,4]
        if(!CollectionUtils.isEmpty(dataIds)){

            for (EcTemplate template : dataIds) {
                addIds.add(template.getId());
                //判断有多少是需要删除的
                if(!templateIds.contains(template.getId().toString())){
                    EcTemplateElementStyle e = new EcTemplateElementStyle();
                    e.setElementStyleGroupId(groupId);
                    e.setTemplateId(template.getId());
                    needDeleteList.add(e);
                }
            }
            //判断有多少是需要 add 的
            for (String tempId : templateIds) {
                if (StringUtils.isNotEmpty(tempId)){
                    Long templateId = Long.parseLong(tempId);
                    if(!addIds.contains(templateId)){
                        EcTemplateElementStyle e = new EcTemplateElementStyle();
                        e.setTemplateId(templateId);
                        e.setElementStyleGroupId(groupId);
                        needAddList.add(e);
                    }
                }
            }
        }else{
            // 如果没有关联，则全部insert
            for (String tempId : templateIds) {
                Long templateId = Long.parseLong(tempId);
                EcTemplateElementStyle e = new EcTemplateElementStyle();
                e.setTemplateId(templateId);
                e.setElementStyleGroupId(groupId);
                needAddList.add(e);
            }
        }
        // needAddIds  insert
        if (needAddList.size() == 0){
            int[] deleteCount = ecTemplateElementStyleRdbService.batchDelete(needDeleteList);
            logger.info("-------------------删除的关联样式数据数量为：" + deleteCount.length + "--------------------------");
            return 0 - deleteCount.length;
        } else  {
            List<Long> styleIds = ecTemplateElementStyleRdbService.batchSave(needAddList);
            int[] deleteCount = ecTemplateElementStyleRdbService.batchDelete(needDeleteList);
            logger.info("-------------------删除的关联样式数据数量为：" + deleteCount.length + "--------------------------");
            return styleIds.size() - deleteCount.length;
        }

    }

    @Override
    public List<EcTemplate> findTemplateByStyleGroupIdAndOrgId(int groupId, Long orgId) {
        return ecTemplateElementStyleRdbService.findTemplateByStyleGroupIdAndOrgId(groupId, orgId);
    }

    @Override
    public List<EcTemplate> findByStyleGroupIdsAndOrgId(Set<Integer> styleGroups, Long orgId) {
        return ecTemplateElementStyleRdbService.findByStyleGroupIdsAndOrgId(styleGroups, orgId);
    }
}
